package com.qyhl.question.question.challenge;


import com.qyhl.question.common.QuestionUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.question.ClassicShareBean;
import com.qyhl.webtv.commonlib.entity.question.QuestionChallengeBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class QuestionChallengeModel implements QuestionChallengeContract.QuestionChallengeModel {
    private QuestionChallengePresenter mPresenter;

    QuestionChallengeModel(QuestionChallengePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void startChallenge(String username, String subjectId) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(QuestionUrl.BASE_QUESTION_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.QUESTION_VERSION);
            object.put("name", "beginPass");
            object.put("username", username);
            object.put("subjectId", subjectId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<QuestionChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            mPresenter.startError("暂无任何题目！");
                        } else {
                            mPresenter.startError("获取题目失败，请稍后再试！");
                        }

                    }

                    @Override
                    public void onSuccess(QuestionChallengeBean bean) {
                        if (bean != null) {
                            mPresenter.startSuccess(bean);
                        } else {
                            mPresenter.startError("暂无任何题目！");
                        }
                    }
                });
    }

    @Override
    public void submit(int recordId, int questionId, String answer, int seconds) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(QuestionUrl.BASE_QUESTION_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.QUESTION_VERSION);
            object.put("name", "answerPassQuestion");
            object.put("recordId", recordId);
            object.put("questionId", questionId);
            object.put("answer", answer);
            object.put("seconds", seconds);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<QuestionChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            mPresenter.submitError("暂无任何题目！");
                        } else {
                            mPresenter.submitError("获取结果失败，请稍后再试！");
                        }

                    }

                    @Override
                    public void onSuccess(QuestionChallengeBean bean) {
                        mPresenter.submitSuccess(bean);
                    }
                });

    }

    @Override
    public void submitTimeOut(int recordId, int questionId, int seconds) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(QuestionUrl.BASE_QUESTION_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.QUESTION_VERSION);
            object.put("name", "passTimeout");
            object.put("recordId", recordId);
            object.put("questionId", questionId);
            object.put("seconds", seconds);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<QuestionChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            mPresenter.timeOutError("暂无任何题目！");
                        } else {
                            mPresenter.timeOutError("获取结果失败，请稍后再试！");
                        }

                    }

                    @Override
                    public void onSuccess(QuestionChallengeBean bean) {
                        mPresenter.timeOutSuccess(bean);
                    }
                });
    }

    @Override
    public void doShare(int recordId) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(QuestionUrl.BASE_QUESTION_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.QUESTION_VERSION);
            object.put("name", "shareResult");
            object.put("recordId", recordId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<ClassicShareBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.shareResult(null, "获取分享信息出错，请稍后再试！");

                    }

                    @Override
                    public void onSuccess(ClassicShareBean bean) {
                        mPresenter.shareResult(bean, "获取分享信息成功！");
                    }
                });

    }
}
